/********************************************************
	Code for Figure 3 & 4
	including lincom results
	Needs to be run before the R syntax
*********************************************************/

/******************************
Experiment 1
******************************/

clear
cd "C:\Users\Joshua\OneDrive\Documents\Work - Finished\BJPS - Comp Explanations\Dataverse"
use exp1_cleaned.dta

****Model
regress therm01 ib3.cond_info##i.cond_party1

		margins, dydx(4.cond_info 5.cond_info 6.cond_info) at(cond_party1 = (1 2)) coeflegend post 
		
		*Within
			*Co-Partisans
				*Teachers-Partisan
				lincomest _b[4.cond_info:1bn._at] - _b[6.cond_info:1bn._at]
				parmest, label saving(exp1_co_teachers, replace)
				

				*Non-Partisan-Partisan
				regress therm01 ib3.cond_info##i.cond_party1
				margins, dydx(4.cond_info 5.cond_info 6.cond_info) at(cond_party1 = (1 2)) coeflegend post 
				
				lincomest _b[5.cond_info:1bn._at] - _b[6.cond_info:1bn._at]
				parmest, label saving(exp1_co_np, replace)
			
			*Opposing Partisans
				*Teachers-Partisan
				regress therm01 ib3.cond_info##i.cond_party1
				margins, dydx(4.cond_info 5.cond_info 6.cond_info) at(cond_party1 = (1 2)) coeflegend post 
				
				lincomest _b[4.cond_info:2._at] - _b[6.cond_info:2._at]
				parmest, label saving(exp1_op_teachers, replace)
				
				*Non-Partisan-Partisan
				regress therm01 ib3.cond_info##i.cond_party1
				margins, dydx(4.cond_info 5.cond_info 6.cond_info) at(cond_party1 = (1 2)) coeflegend post 
				
				lincomest _b[5.cond_info:2._at] - _b[6.cond_info:2._at]
				parmest, label saving(exp1_op_np, replace)
		
		*Between
			regress therm01 ib3.cond_info##i.cond_party1
			margins, dydx(4.cond_info 5.cond_info 6.cond_info) at(cond_party1 = (1 2)) coeflegend post 
			
			*(Co-Partisan: Teacher - Partisan) - (Opposing Partisan: Teacher - Partisan)
			mlincom (1-5) - (2 - 6), stats(all)
			
			matrix rename _mlincom exp1_mat_teacher
			
			xsvmat double exp1_mat_teacher, rownames(param) rowlabels(varlab) saving(exp1_mat_teacher.dta, replace)
			
			*(Co-Partisan: Non-Partisan - Partisan)....
			mlincom (3-5) - (4-6), stats(all)
			
			matrix rename _mlincom exp1_mat_np
			
			xsvmat double exp1_mat_np, rownames(param) rowlabels(varlab) saving(exp1_mat_np.dta, replace)
			
/******************************
Experiment 2
******************************/

clear
cd "C:\Users\Joshua\OneDrive\Documents\Work - Finished\BJPS - Comp Explanations\Dataverse"
use exp2_cleaned.dta

***Model
regress therm_post01 ib2.treat_expl##i.copartisan therm_pre01
		
		margins, dydx(3.treat_expl 4.treat_expl) at(copartisan = (1 2)) coeflegend post 
		
		*Within
			*Co-Partisans
				*HC - LC
				test _b[3.treat_expl:1bn._at] = _b[4.treat_expl:1bn._at]
				lincomest _b[3.treat_expl:1bn._at] - _b[4.treat_expl:1bn._at]
				parmest, label saving(exp2_co_hc, replace)
			
			*Opposing Partisans
				*HC - LC
				regress therm_post01 ib2.treat_expl##i.copartisan therm_pre01
				margins, dydx(3.treat_expl 4.treat_expl) at(copartisan = (1 2)) coeflegend post 
		
				test _b[3.treat_expl:2._at] = _b[4.treat_expl:2._at]
				lincomest _b[3.treat_expl:2._at] - _b[4.treat_expl:2._at]
				parmest, label saving(exp2_op_hc, replace)
				
		*Between
			regress therm_post01 ib2.treat_expl##i.copartisan therm_pre01
			margins, dydx(3.treat_expl 4.treat_expl) at(copartisan = (1 2)) coeflegend post 
			
			*(Co-Partisan: HC-LC) - (Opposing Partisan: HC-LC)
			mlincom (1-3) - (2-4), stats(all)
				
			matrix rename _mlincom exp2_mat_hc
			
			xsvmat double exp2_mat_hc, rownames(param) rowlabels(varlab) saving(exp2_mat_hc.dta, replace)
			

/******************************
Experiment 3a 
******************************/
clear
cd "C:\Users\Joshua\OneDrive\Documents\Work - Finished\BJPS - Comp Explanations\Dataverse"
use exp3_cleaned.dta
set more off

****Gillibrand
eststo clear
eststo: regress gil_post01 ib2.gil_exp##i.gil_partisan i.gil_pre i.gil_prox i.treat_order

		margins, dydx(3.gil_exp 4.gil_exp) at(gil_partisan=(1 2)) post coeflegend
		
		*Within
			*Co-Partisans: LW - RW
			test _b[3.gil_exp:1bn._at] = _b[4.gil_exp:1bn._at]
			lincomest _b[3.gil_exp:1bn._at] - _b[4.gil_exp:1bn._at]
			parmest, label saving(exp3_gil_co_hc, replace)

			*Opposing: LW-RW
			 regress gil_post01 ib2.gil_exp##i.gil_partisan i.gil_pre i.gil_prox i.treat_order
			 margins, dydx(3.gil_exp 4.gil_exp) at(gil_partisan=(1 2)) post coeflegend
			 
			 test _b[3.gil_exp:2._at] = _b[4.gil_exp:2._at]
			 lincomest _b[3.gil_exp:2._at] - _b[4.gil_exp:2._at]
			 parmest, label saving(exp3_gil_op_hc, replace)

		*Diff in Diff: (Co-Partisan: LW - RW) - (Opposing Partisan: RW-LW)
			 regress gil_post01 ib2.gil_exp##i.gil_partisan i.gil_pre i.gil_prox i.treat_order
			 margins, dydx(3.gil_exp 4.gil_exp) at(gil_partisan=(1 2)) post coeflegend
			 
			 mlincom (1 - 3) - (2-4), stats(all)
			
			matrix rename _mlincom exp3_gil_mat_lwrw
			
			xsvmat double exp3_gil_mat_lwrw, rownames(param) rowlabels(varlab) saving(exp3_gil_mat_lwrw.dta, replace)
			

/******************************
Experiment 3b
******************************/

clear
cd "C:\Users\Joshua\OneDrive\Documents\Work - Finished\BJPS - Comp Explanations\Dataverse"
use exp3_cleaned.dta
set more off

****Corker
eststo clear
eststo: regress corker_post01 ib2.corker_exp##i.corker_partisan i.corker_pre  i.corker_prox i.treat_order

	margins, dydx(3.corker_exp 4.corker_exp) at(corker_partisan=(1 2)) post coeflegend
		
		*Within
			*Co-Partisans: LW - RW
			test _b[3.corker_exp:1bn._at] = _b[4.corker_exp:1bn._at]
			lincomest _b[3.corker_exp:1bn._at] - _b[4.corker_exp:1bn._at]
			parmest, label saving(exp3_corker_co_hc, replace)

			*Opposing: LW-RW
			 regress corker_post01 ib2.corker_exp##i.corker_partisan i.corker_pre i.corker_prox i.treat_order
			 margins, dydx(3.corker_exp 4.corker_exp) at(corker_partisan=(1 2)) post coeflegend
			 
			 test _b[3.corker_exp:2._at] = _b[4.corker_exp:2._at]
			 lincomest _b[3.corker_exp:2._at] - _b[4.corker_exp:2._at]
			 parmest, label saving(exp3_corker_op_hc, replace)

		*Diff in Diff: (Co-Partisan: LW - RW) - (Opposing Partisan: RW-LW)
			 regress corker_post01 ib2.corker_exp##i.corker_partisan i.corker_pre i.corker_prox i.treat_order
			 margins, dydx(3.corker_exp 4.corker_exp) at(corker_partisan=(1 2)) post coeflegend
			 
			 mlincom (1 - 3) - (2-4), stats(all)
			
			matrix rename _mlincom exp3_corker_mat_lwrw
			
			xsvmat double exp3_corker_mat_lwrw, rownames(param) rowlabels(varlab) saving(exp3_corker_mat_lwrw.dta, replace)
			